<?php
/*
 * Page's Class 
 * NUM_PAR_PAGE:每页显示的记录数目
 */
class Model_Page
{
	private $total_page;			
    private $total_nombre;			#All records
    private $page_courante;			#Current page
    private $page_pre;				#Previous page
    private $page_next;				#Next page
    private $page_first;
    private $page_end;
    private $_skip;					#Skip page
    private $page_css;				#Css page
    private $_other;				#Other Information
    private $_request;				
    private $page_i;           //Start page
	private $page_ub;          //End page
	
    /**
     * 
     * @param $total_nombre(页面总数)
     * @param $request()
     * @param $num_per_page(每页显示的记录数目)
     */
 	function __construct($total_nombre,$request,$num_per_page=10)
    {
      	$this->_request = clone $request;
      	$this->page_courante = $this->_request->getParam("page")?$this->_request->getParam("page"):0;
  		$this->total_page = ceil($total_nombre/$num_per_page);
  		$this->total_nombre = $total_nombre;
  		
    	$this -> page_i = $this->page_courante  - 2;
        $this -> page_ub = $this->page_courante  + 2;
        if($this -> page_i < 1){
            $this -> page_ub = $this -> page_ub + (1 - $this -> page_i);
            $this -> page_i = 1;
        }
        if($this -> page_ub > $this->total_page ){
            $this -> page_i = $this -> page_i - ($this -> page_ub - $this->total_page );
            $this -> page_ub = $this->total_page ;
            if($this -> page_i < 1) $this -> page_i = 1;
        }
    }
	


 	function render()
    {
  		$html = '<div class="pages">';
  		$html .= $this->get_other();
  		if($this->total_page > 1)
  		{
  			$html .= $this->getPage_first();
  			#Previous Page
   			$html .= $this->getPage_pre();
   			
//   			$this->_request->setParam("page",0);
//   			$html .=$this->page_courante - 2 > 1 && $this->total_page > 4 ? '<a href="'.$this->rebuildUrl().'" class="first">1 ...</a>' : '';
  		
   			#Page List
//   			for($i=0;$i<$this->total_page;$i++)
   			for($i=$this->page_i;$i<$this->page_ub;$i++)
   			{
    			if($i>0)$html.= " ";
    			$this->_request->setParam("page",$i);
    			if($i==$this->page_courante){
    				$html .= '<strong>'.($i+1).'</strong>';
    			}else{
    				$html.= '<a href="'.$this->rebuildUrl().'">'.($i+1).'</a>';
    			}
   			}
   			
   			#Next Page
   			$html .= $this->getPage_next();
   			#End Page
   			$html .= $this->getPage_end();
   			#Skip Page
   			$html .= $this->get_skip();
  		}
  		
  		$html .= '</div>';
  		$html .= $this->getPage_css();
  		return $html;
    }

    
    function rebuildUrl()
    {
      	$url = $this->_request->getBaseUrl();
      	$params = $this->_request->getParams();
      	
      	$url .="/".$params["module"];
      	$url .="/".$params["controller"];
      	$url .="/".$params["action"];
      	foreach ( $params as $key=>$value)
      	{
      		$pos = strpos($key,'/');
       		if(!in_array($key,array("module","controller","action")) && $pos === false)
       			$url.="/".$key."/".$value;
      	}
      	//Zend_Debug::dump($params); 
      	return $url;
    }

	/**
	 * @return the $page_css
	 */
	public function getPage_css() {
		$page_css = '<style>';
		$page_css .= '.pages{float:right;padding:2px 0;}';
		$page_css .= '.pages a,.pages strong{text-decoration:none;background-color:#FFFFFF;color:#006699;border:1px solid #E8E8E8;float:left;height:26px;line-height:26px;margin-right:2px;overflow:hidden;padding:0 6px;}';
		$page_css .= '.pages strong{background-color:#006699;color:#FFFFFF;font-weight:700}';
		$page_css .= '.pages a:hover{border-color:#ffffff;color:#006699;border:1px solid;float:left;height:26px;line-height:26px;margin-right:2px;overflow:hidden;padding:0 6px;}';
		$page_css .= '.pages a .next{background-image:url(/public/images/arrow_right.gif);background-position:90% 50%;padding-right:16px;}';
		$page_css .= '.pages a .prev{background-image:url(/public/images/arrow_left.gif);background-position:90% 50%;padding-right:16px;}';
		$page_css .= '.summary{float:left;font-size:13px;margin-top:5px;margin-right:10px;}';
		$page_css .= '.summary b{font-size:15px;}';
		$page_css .= '</style>';
		$this->page_css = $page_css;
		return $this->page_css;
	}
	
	/**
	 * Other Information
	 */
	public function get_other() {
		$html = '<span class="summary">共有<b>'.$this->total_nombre.'</b>条记录，当前第 <b>'.($this->page_courante+1).'</b>页，共<b> '.$this->total_page.'</b>页</span>';
		return $html;
	}
	

	/**
	 * Next Page
	 */
	public function getPage_next() {
		$this->_request->setParam("page",$this->page_courante+1);
		if($this->page_courante < $this->total_page-1)
			$html =  '<a class="next" href="'.$this->rebuildUrl().'">下一页 </a>';
		return $html;
	}

	/**
	 * Previou Page
	 */
	public function getPage_pre() {
		$html='';
		$this->_request->setParam("page",$this->page_courante-1);	
		if($this->page_courante > 0 && $this->page_courante <= $this->total_page )
			$html .=  '<a class="prev" href="'.$this->rebuildUrl().'"><img src="/public/images/arrow_left.gif" style="border:0;margin-top:10px;"/></a>';
			
		return $html;
	}

	/**
	 * First Page
	 */
	public function getPage_first() {
		$this->_request->setParam("page",0);
		$html =  '<a  href="'.$this->rebuildUrl().'">首页 </a>';
		return $html;
	}
	
	/**
	 * End Page
	 */
	public function getPage_end() {
		
		$this->_request->setParam("page",$this->total_page-1);
		$html =  '<a  href="'.$this->rebuildUrl().'">末页 </a>';
		return $html;
	}
      
	/**
	 * Skip Page
	 */
	public function get_skip() {
		$url = $this->_request->getBaseUrl();
      	$params = $this->_request->getParams();
		$url .="/".$params["module"];
      	$url .="/".$params["controller"];
      	$url .="/".$params["action"];
      	foreach ( $params as $key=>$value)
      	{
      		$pos = strpos($key,'/');
       		if(!in_array($key,array("module","controller","action")) && $pos === false && $key!='page')
       			$url.="/".$key."/".$value;
      	}
      	
		$html='';
		$totalPageNum = $this->total_page;
        $currentPage = $this->page_courante+1 ;
        $html = '<div class="summary">跳至<select name="page" onchange="window.location.href=\'' . $url. '/page/\'+(this.value-1)">';
        for( $i = 1;$i <= $totalPageNum;$i++ )
        {
            if ( $currentPage == $i )
            {
        		$html .= '<option selected="selected" value="' . ($i) . '">' . ($i ). '</option>';
            }else{
            	$html .= '<option value="' . ($i) . '">' . ($i) . '</option>';
            }
        }
        $html .= '</select></div>';
		return $html;
	}
   
}